<template>
  <el-dialog
  :title="typeName +'：'+ typeCode"
  :close-on-click-modal="false"
  :visible.sync="visible">
    <div style="margin-bottom: 5px;">
      <el-button
        size="mini"
        type="primary"
        icon="el-icon-circle-plus-outline"
        @click="addRow">新增子项</el-button>
    </div>
    <div>
      <el-table
        border
        class="template-list"
        :data="dataList"
        style="width: 100%;">
        <el-table-column
          label="字典项代码"
          header-align="center"
          align="center">
          <template slot-scope="scope">
            <div v-if="!scope.row.editing">
              <span>{{ scope.row.constantCode }}</span>
            </div>
            <div v-else>
              <el-input size="mini" v-model="scope.row.constantCode" placeholder="字典项代码"></el-input>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="字典项名称"
          header-align="center"
          align="center">
          <template slot-scope="scope">
            <div v-if="!scope.row.editing">
              <span>{{ scope.row.constantName }}</span>
            </div>
            <div v-else>
              <el-input size="mini" v-model="scope.row.constantName" placeholder="字典项名称"></el-input>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="排序号"
          header-align="center"
          align="center">
          <template slot-scope="scope">
            <div v-if="!scope.row.editing">
              <span>{{ scope.row.orderNum }}</span>
            </div>
            <div v-else>
              <el-input size="mini" v-model="scope.row.orderNum" placeholder="排序号"></el-input>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          header-align="center"
          align="center"
          width="190">
          <template slot-scope="scope">
            <div class="operate-groups">
              <el-button
                style="padding: 3px;"
                type="primary"
                size="mini"
                v-if="!scope.row.editing"
                icon="el-icon-edit-outline"
                @click="handleEdit(scope.$index, scope.row)">编辑
              </el-button>
              <el-button
                style="padding: 3px;"
                type="primary"
                size="mini"
                v-if="scope.row.editing"
                icon="el-icon-success"
                @click="handleSave(scope.$index, scope.row)">保存
              </el-button>
              <el-button
                style="padding: 3px;"
                size="mini"
                type="danger"
                v-if="!scope.row.editing"
                icon="el-icon-delete"
                @click="handleDelete(scope.$index, scope.row)">删除
              </el-button>
              <el-button
                style="padding: 3px;"
                size="mini"
                type="warning"
                v-if="scope.row.editing"
                icon="el-icon-warning"
                @click="handleCancel(scope.$index, scope.row)">取消
              </el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      typeCode: '',
      typeName: '',
      dataList: []
    }
  },
  methods: {
    // 获取数据列表
    init (typeCode, typeName) {
      this.visible = true
      this.typeCode = typeCode
      this.typeName = typeName
      this.$http({
        url: this.$http.requestUrl('/sys/constantcode/list'),
        method: 'get',
        params: this.$http.requestParams({
          'typeCode': typeCode
        })
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.dataList = data.list
        } else {
          this.dataList = []
        }
      })
    },
    // 编辑
    handleEdit ($index, row) {
      this.$set(this.dataList[$index], 'editing', true)
    },
    // 保存
    handleSave ($index, row) {
      this.$set(this.dataList[$index], 'editing', false)
      this.$http({
        url: this.$http.requestUrl(`/sys/constantcode/${!row.id ? 'save' : 'update'}`),
        method: 'post',
        data: this.$http.requestData({
          'id': row.id || undefined,
          'typeCode': this.typeCode,
          'constantCode': row.constantCode,
          'constantName': row.constantName,
          'orderNum': row.orderNum
        })
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.init(this.typeCode, this.typeName)
            }
          })
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    // 取消
    handleCancel ($index, row) {
      this.$set(this.dataList[$index], 'editing', false)
    },
    // 新增一条数据
    addRow () {
      // debugger
      this.dataList = this.dataList || []
      this.dataList.push({
        constantCode: '',
        constantName: '',
        orderNum: '',
        editing: true
      })
    },
    // 删除
    handleDelete ($index, row) {
      this.$confirm(`是否删除${row.constantName}?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.dataList.splice($index, 1)
        if (!row.id) {
          return
        }
        this.$http({
          url: this.$http.requestUrl('/sys/constantcode/delete'),
          method: 'post',
          data: this.$http.requestData(row.id, false)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '删除成功!',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      }).catch((err) => {
        this.$message({
          type: 'error',
          message: err
        })
      })
    }
  }
}
</script>
